package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.validation.constraints.Positive;

@Configuration
public class MyRabbitMq {


    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     *   序列化  传送的类  可视化
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    @PostConstruct
    public void myRabbitTemplate(){
        // 开启消息确认机制
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             *
             * @param correlationData 表示 消息的唯一标识
             * @param b  投放消息到 交换机成功否
             * @param s   错误信息
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {

            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            /**
             * 注意  改方法是指 消息 回退到队列失败
             * @param message  回退的失败信息
             * @param i
             * @param s
             * @param s1
             * @param s2
             */
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {

            }
        });


    }



}
